package com.juqimiao.raccoon.commodity.command;

import com.juqimiao.linn.command.CommandExecutor;
import com.juqimiao.linn.command.CommandExecutorI;
import com.juqimiao.linn.exception.BusinessException;
import com.juqimiao.linn.exception.SystemException;
import com.juqimiao.linn.extension.ExtensionExecutor;
import com.juqimiao.linn.transfer.Response;
import com.juqimiao.linn.transfer.query.builders.ResponseBuilder;
import com.juqimiao.raccoon.commodity.appobj.CategoryGroupSortAO;
import com.juqimiao.raccoon.commodity.domain.CategoryGroupFactory;
import com.juqimiao.raccoon.commodity.domain.category.CategoryGroup;
import com.juqimiao.raccoon.commodity.dto.SortCatgoryGroupCmd;
import com.juqimiao.raccoon.commodity.extension.CategoryGroupValidExtPt;
import com.juqimiao.raccoon.commodity.repository.CategoryGroupRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@CommandExecutor
public class SortCatgoryGroupCmdExt
        implements CommandExecutorI<Response, SortCatgoryGroupCmd> {

    private ExtensionExecutor extensionExecutor;
    private CategoryGroupRepository repository;
    private CategoryGroupFactory factory;

    @Autowired
    public SortCatgoryGroupCmdExt(ExtensionExecutor extensionExecutor,
                                  CategoryGroupRepository repository,
                                  CategoryGroupFactory factory) {
        this.extensionExecutor = extensionExecutor;
        this.repository = repository;
        this.factory = factory;
    }

    @Override
    public Response execute(SortCatgoryGroupCmd cmd) {
        extensionExecutor.executeVoid(CategoryGroupValidExtPt.class,
                extension -> extension.validate(cmd));
        List<String> idList = new ArrayList<>();
        String[] ids = new String[cmd.getCategoryGroupList().size()];
        for (int i = 0; i < cmd.getCategoryGroupList().size(); i++) {
            ids[i] = cmd.getCategoryGroupList().get(i).getId();
        }

        List<CategoryGroup> groups = factory.createMany(repository, ids);

        if (groups == null
                || groups.isEmpty()
                || groups.size() != cmd.getCategoryGroupList().size()) {
            throw new BusinessException("指定的类目组不存在。");
        }

        for (CategoryGroup group : groups) {
            CategoryGroupSortAO result = cmd.getCategoryGroupList()
                    .stream()
                    .filter(ao -> ao.getId().equals(group.getId()))
                    .findFirst()
                    .orElse(null);
            if (result == null) {
                throw new BusinessException(String.format("Id为%s的类目并不存在。", group.getId()));
            }

            group.setSortOrder(result.getSortOrder());
        }

        Collections.sort(groups);

        int index = 1;

        for (CategoryGroup group : groups) {
            group.setSortOrder(index);
            index++;
        }

        int count = repository.batchUpdateSortOrder(groups);

        if (count <= 0) {
            throw new SystemException("Failure to batchUpdate groups.");
        }

        return ResponseBuilder.builder().success("The group was sorted successfully.").build();
    }
}
